package com.meituan.catering.management.product.biz.model.converter;

import com.meituan.catering.management.common.model.api.http.AuditingHttpModel;
import com.meituan.catering.management.common.model.biz.AuditingBO;
import com.meituan.catering.management.product.api.http.model.dto.ProductDetailHttpDTO;
import com.meituan.catering.management.product.biz.model.ProductAccessoryBO;
import com.meituan.catering.management.product.biz.model.ProductDetailBO;
import com.meituan.catering.management.product.biz.model.ProductMethodBO;

import java.util.LinkedList;
import java.util.List;

/**
 * @author Believe
 * Date 2022/5/22 13:23
 */
public class ProductDetailHttpDTOConverter {


    public static ProductDetailHttpDTO toProductDetailHttpResponse(ProductDetailBO productDetailBO) {
        ProductDetailHttpDTO productDetailHttpDTO = new ProductDetailHttpDTO();
        productDetailHttpDTO.setId(productDetailBO.getProductBO().getId());
        productDetailHttpDTO.setVersion(productDetailBO.getProductBO().getVersion());
        productDetailHttpDTO.setTenantId(productDetailBO.getProductBO().getTenantId());
        productDetailHttpDTO.setEnabled(productDetailBO.getProductBO().getEnabled());
        productDetailHttpDTO.setName(productDetailBO.getProductBO().getName());
        productDetailHttpDTO.setUnitPrice(productDetailBO.getProductBO().getUnitPrice());
        productDetailHttpDTO.setMinSalesQuantity(productDetailBO.getProductBO().getMinSalesQuantity());
        productDetailHttpDTO.setIncreaseSalesQuantity(productDetailBO.getProductBO().getIncreaseSalesQuantity());
        productDetailHttpDTO.setUnitOfMeasure(productDetailBO.getProductBO().getUnitOfMeasure());
        productDetailHttpDTO.setDescription(productDetailBO.getProductBO().getDescription());
        productDetailHttpDTO.setAuditing(toAuditingModel(productDetailBO.getProductBO().getAuditing()));
        productDetailHttpDTO.setAccessoryGroups(toDetailDTOAccessoryGroup(productDetailBO.getAccessoryBOList()));
        productDetailHttpDTO.setMethodGroups(toDetailDTOMethodGroup(productDetailBO.getMethodBOList()));
        return productDetailHttpDTO;
    }

    public static List<ProductDetailHttpDTO.AccessoryGroup> toDetailDTOAccessoryGroup(List<ProductAccessoryBO> accessoryBOList) {
        List<ProductDetailHttpDTO.AccessoryGroup> accessoryGroupList = new LinkedList<>();
        for (ProductAccessoryBO productAccessoryBO : accessoryBOList) {
            ProductDetailHttpDTO.AccessoryGroup accessoryGroup = new ProductDetailHttpDTO.AccessoryGroup();
            accessoryGroup.setName(productAccessoryBO.getGroupName());
            accessoryGroup.setOptions(toAccessoryOption(productAccessoryBO));
            accessoryGroupList.add(accessoryGroup);
        }
        return accessoryGroupList;
    }

    public static List<ProductDetailHttpDTO.MethodGroup> toDetailDTOMethodGroup(List<ProductMethodBO> methodBOList) {
        List<ProductDetailHttpDTO.MethodGroup> methodGroupList = new LinkedList<>();
        for (ProductMethodBO productMethodBO : methodBOList) {
            ProductDetailHttpDTO.MethodGroup methodGroup = new ProductDetailHttpDTO.MethodGroup();
            methodGroup.setName(productMethodBO.getGroupName());
            methodGroup.setOptions(toMethodOption(productMethodBO));
            methodGroupList.add(methodGroup);
        }
        return methodGroupList;
    }

    public static List<ProductDetailHttpDTO.MethodGroup.Option> toMethodOption(ProductMethodBO option) {
        List<ProductDetailHttpDTO.MethodGroup.Option> optionBOList = new LinkedList<>();
        ProductDetailHttpDTO.MethodGroup.Option optionGroup = new ProductDetailHttpDTO.MethodGroup.Option();
        optionGroup.setName(option.getName());
        optionGroup.setId(option.getId());
        optionBOList.add(optionGroup);
        return optionBOList;
    }

    public static List<ProductDetailHttpDTO.AccessoryGroup.Option> toAccessoryOption(ProductAccessoryBO accessory) {
        List<ProductDetailHttpDTO.AccessoryGroup.Option> options = new LinkedList<>();
        ProductDetailHttpDTO.AccessoryGroup.Option option = new ProductDetailHttpDTO.AccessoryGroup.Option();
        option.setId(accessory.getId());
        option.setName(accessory.getName());
        option.setUnitPrice(accessory.getUnitPrice());
        option.setUnitOfMeasure(accessory.getUnitOfMeasure());
        options.add(option);
        return options;
    }


    public static AuditingHttpModel toAuditingModel(AuditingBO auditingBO) {
        AuditingHttpModel auditingHttpModel = new AuditingHttpModel();
        auditingHttpModel.setCreatedBy(auditingBO.getCreatedBy());
        auditingHttpModel.setCreatedAt(auditingBO.getCreatedAt());
        auditingHttpModel.setLastModifiedBy(auditingBO.getLastModifiedBy());
        auditingHttpModel.setLastModifiedAt(auditingBO.getLastModifiedAt());
        return auditingHttpModel;
    }

}
